package com.srwl.mytx.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Build;

import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.view.ViewCompat;

public class BottomBarBadge extends AppCompatTextView {
    private static final String TAG = "BottomBarBadge";
    private int count;
    private boolean isVisible;

    BottomBarBadge(Context context) {
        super(context);
    }

    public BottomBarBadge(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BottomBarBadge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
//        int specHeight = MeasureSpec.getSize(heightMeasureSpec);
//        if (specWidth < specHeight) {
//            specWidth = specHeight;
//        }
//        int width = (int) getPaint().measureText(getText().toString()) + getPaddingLeft() + getPaddingRight();
//        setMeasuredDimension(width, specHeight);
//    }

    private void init() {
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                adjustPositionAndSize();
            }
        });
    }


    void setCount(int count) {
        this.count = count;
        setText(String.valueOf(count));

    }


    int getCount() {
        return count;
    }


    public void show() {
        isVisible = true;
        setVisibility(VISIBLE);
        ViewCompat.animate(this)
                .setDuration(150)
                .alpha(1)
                .scaleX(1)
                .scaleY(1)
                .start();
    }


    public void hide() {
        if (!isVisible) {
            return;
        }
        isVisible = false;
        ViewCompat.animate(this)
                .setDuration(150)
                .alpha(0)
                .scaleX(0)
                .scaleY(0)
                .start();
        setVisibility(INVISIBLE);
    }


    boolean isVisible() {
        return isVisible;
    }


    void adjustPositionAndSize() {
        // AppCompatImageView iconView = tab.getIconView();
        ViewGroup.LayoutParams params = getLayoutParams();

        int size = Math.max(getWidth(), getHeight());
        Log.d(TAG, "adjustPositionAndSize: getWidth()" + getWidth() + "getHeight()" + getHeight());
//        float xOffset = (float) (iconView.getWidth() / 1.25);
//
//        setX(iconView.getX() + xOffset);
//        setTranslationY(10);

        if (params.width != size || params.height != size) {
            params.width = size;
            params.height = size;
            setLayoutParams(params);
        }
    }


}
